package com.crwal.app.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.crwal.app.bean.CrwalUrl;
import com.crwal.app.bean.News;
import com.crwal.app.service.WebCrwalService;
import com.crwal.app.utils.ExcelUtils;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
 
@Component
@Slf4j
public class MyCrwalAsyncTask {
	
	@Autowired
	private WebCrwalService service;
	
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String natureworksllc(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.natureworksllc(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
			log.debug("抓取["+companyName+"]页面增量数据任务结束!");
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String corbion(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.corbion(companyName, companyHref, maxDate);
        	
        	this.export(newsList, existList, fileName, flag);
			log.debug("抓取["+companyName+"]页面增量数据任务结束!");
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String totalcorbion(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.totalcorbion(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
			log.debug("抓取["+companyName+"]页面增量数据任务结束!");
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String pttmcc(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.pttmcc(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
			log.debug("抓取["+companyName+"]页面增量数据任务结束!");
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }

    @Async("taskExecutor")  //使用taskExecutor线程池
    public String cjbio(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.cjbio(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
			log.debug("抓取["+companyName+"]页面增量数据任务结束!");
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String basf(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.basf(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
			log.debug("抓取["+companyName+"]页面增量数据任务结束!");
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String arkema(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.arkema(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String plasticker(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.plasticker(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String bioplasticsnews(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.bioplasticsnews(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String bioplasticsmagazine(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.bioplasticsmagazine(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String plasticsnews(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.plasticsnews(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    @Async("taskExecutor")  //使用taskExecutor线程池
    public String biobased(String companyName, String companyHref, String fileName, String maxDate,
    		boolean flag, List<News> existList) {
    	List<News> newsList = new ArrayList<News>();
        try {
        	newsList = service.biobased(companyName, companyHref, maxDate);
        	this.export(newsList, existList, fileName, flag);
        } catch (Exception e) {
        	log.error("抓取["+companyName+"]增量数据异常！",e);
        }
        return null;
    }
    
    
    private void export(List<News> newsList, List<News> existList, String fileName, boolean flag) {
    	if(null != newsList && newsList.size() > 0) {
			existList.addAll(newsList);
			//先删除原来的文件，在新建文件进行导出
			if(flag && (existList != null && existList.size() > 0)) {
				String path = CrwalUrl.getTempLocDirectory()+fileName;
				boolean tempFlag = FileUtil.del(path);
				if(tempFlag) {
	        		ExcelUtils.exportCrwalResult(CrwalUrl.getTempLocDirectory(), fileName, existList);
	        	}
			}
		}
    }
    
}